<template>
  <div class="dashboard">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="clearfix">
              <span>用户仪表板</span>
              <el-button style="float: right; padding: 3px 0" type="text" @click="logout">
                退出登录
              </el-button>
            </div>
          </template>
          <div class="dashboard-content">
            <el-row :gutter="20">
              <el-col :span="8">
                <el-card class="info-card">
                  <template #header>
                    <span>用户信息</span>
                  </template>
                  <div v-if="currentUser">
                    <p><strong>用户名：</strong>{{ currentUser.username }}</p>
                    <p><strong>角色：</strong>{{ getRoleName(currentUser.role) }}</p>
                    <p><strong>邮箱：</strong>{{ currentUser.email }}</p>
                  </div>
                </el-card>
              </el-col>
              <el-col :span="8">
                <el-card class="stats-card">
                  <template #header>
                    <span>系统统计</span>
                  </template>
                  <div>
                    <p><strong>登录状态：</strong>已登录</p>
                    <p><strong>权限级别：</strong>{{ getRoleName(currentUser?.role) }}</p>
                  </div>
                </el-card>
              </el-col>
              <el-col :span="8">
                <el-card class="actions-card">
                  <template #header>
                    <span>快速操作</span>
                  </template>
                  <div>
                    <el-button type="primary" @click="$router.push('/')">返回首页</el-button>
                    <el-button type="success" style="margin-top: 10px;" @click="$router.push('/users')">用户管理</el-button>
                  </div>
                </el-card>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'Dashboard',
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const currentUser = computed(() => store.getters.currentUser)
    const isAuthenticated = computed(() => store.getters.isAuthenticated)
    
    const getRoleName = (role) => {
      const roleNames = {
        'admin': '管理员',
        'teacher': '教师',
        'student': '学生'
      }
      return roleNames[role] || '未知'
    }
    
    const logout = () => {
      store.dispatch('logout')
      router.push('/login')
      ElMessage.success('已成功退出登录')
    }
    
    return {
      currentUser,
      isAuthenticated,
      getRoleName,
      logout
    }
  }
}
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.dashboard-content {
  margin-top: 20px;
}

.info-card, .stats-card, .actions-card {
  height: 200px;
}

.info-card p, .stats-card p {
  margin: 10px 0;
  color: #606266;
}

.actions-card .el-button {
  width: 100%;
}
</style> 